package copyengine.ui.display.movieClip
{
    import copyengine.infoTree.meta.CESAnimationEachFrameInfoMeta;
    import copyengine.infoTree.meta.CESDisplayObjectMeta;

    import starling.display.DisplayObject;

    /**
     * 每个CEMovieClip的子元件 均用该类做一个封装
     * 把meta文件和build出来的mc 放在一起
     * 因为MovieClip每次播放时候均需要用到meta里面的info对该mc进行操作
     */
    public class CEMovieClipChild
    {
        private var mCurrentFrame:int;

        private var mTargetMc:DisplayObject;
        private var mTargetMeta:CESDisplayObjectMeta;

        public function CEMovieClipChild()
        {
        }

        public function initialzie(_targetMc:DisplayObject, _targetMeta:CESDisplayObjectMeta):void
        {
            mTargetMc = _targetMc;
            mTargetMeta = _targetMeta;
            mCurrentFrame = 0;
        }

        /**
         * 将当前的ChildMc转换为某一帧的样式
         */
        public function changeDisplayToFrame(_frameIndex:int):void
        {
            if (_frameIndex != mCurrentFrame) {
                mCurrentFrame = _frameIndex;
                var frameInfo:CESAnimationEachFrameInfoMeta = findCurrentFrameInfoMeta();
                if (frameInfo != null) {
                    mTargetMc.visible = true;
                    mTargetMc.transformationMatrix = frameInfo.getMatrix();
                    mTargetMc.alpha = frameInfo.alpha;
                }
                else {
                    mTargetMc.visible = false;
                }
            }
        }

        public function getTarget():DisplayObject { return mTargetMc; }

        private function findCurrentFrameInfoMeta():CESAnimationEachFrameInfoMeta
        {
            var length:int = mTargetMeta.frameTransformMatrixArray.length;
            for (var i:int = 0; i < length; i++) {
                var infoMeta:CESAnimationEachFrameInfoMeta = mTargetMeta.frameTransformMatrixArray[i];
                if (mCurrentFrame >= infoMeta.startIndex && mCurrentFrame <= infoMeta.endIndex) {
                    return infoMeta;
                }
            }
            return null;
        }

    }
}
